<?php
include('system/sql.php');
// 获取并过滤参数
$wbtreeid = isset($_GET['wbtreeid']) ? intval($_GET['wbtreeid']) : 0;
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

// 查询栏目名称
$menu_name = '';
$stmt_menu = mysqli_prepare($link, "SELECT menu FROM article_menu WHERE wbtreeid = ?");
mysqli_stmt_bind_param($stmt_menu, "i", $wbtreeid);
mysqli_stmt_execute($stmt_menu);
$menu_result = mysqli_stmt_get_result($stmt_menu);

if (!$menu_result || mysqli_num_rows($menu_result) === 0) {
    // 若栏目名称查询不到结果，输出错误信息并终止程序
    echo "未查询到对应的栏目名称，wbtreeid: $wbtreeid", E_USER_ERROR;
}

if ($menu_row = mysqli_fetch_assoc($menu_result)) {
    $menu_name = $menu_row['menu'];
}
mysqli_stmt_close($stmt_menu);

// 查询文章详情
$article = [];
$stmt_article = mysqli_prepare($link, "SELECT * FROM article_article WHERE wbtreeid = ? AND id = ?");
mysqli_stmt_bind_param($stmt_article, "ii", $wbtreeid, $id);
mysqli_stmt_execute($stmt_article);
$article_result = mysqli_stmt_get_result($stmt_article);

if (!$article_result || mysqli_num_rows($article_result) === 0) {
    // 若文章详情查询不到结果，输出错误信息并终止程序
    echo "未查询到对应的文章详情，wbtreeid: $wbtreeid，id: $id", E_USER_ERROR;
    exit;
}

if ($article_row = mysqli_fetch_assoc($article_result)) {
    $article = $article_row;
}
$webinfo_sql = "select * from webinfo";
$webinfo_stmt = $link->prepare($webinfo_sql);
$webinfo_stmt->execute();
$webinfo_result = $webinfo_stmt->get_result();
$webinfo_row = $webinfo_result->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $article['title'] . " - " . $webinfo_row['webname']; ?></title>
    <!-- Bootstrap 5 CSS -->
    <link href="/static/bootstrap.min.css" rel="stylesheet">
    <!-- 自定义样式 -->
    <style>
        img {
            max-width: 60%;
        }

        .article_content {
            font-size: 18px;
        }

        .article-bg-container {
            padding: 5rem;
            background-position: center;
            background-size: cover;
            min-height: 200px;
        }

        .default-bg {
            background-color: #f0f0f0;
        }
        
        .article_content p,
        #article_content p {
            text-indent: 2em;
        }
    </style>
</head>

<body>

    <?php include('nav.php'); ?>
    <div class="article-bg-container default-bg">
        <div class="text-center">
            <h1><?= htmlspecialchars($article['title']) ?></h1>
        </div>
    </div>

    <div class="container my-5">
        <div class="row">
            <div class="col">
                <!-- 面包屑导航 -->
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="/">首页</a></li>
                        <li class="breadcrumb-item">

                            <?php
                            // 获取伪静态配置（假设已从数据库读取到$webinfo_row）
                            $is_static = isset($webinfo_row['static']) ? $webinfo_row['static'] : 0;

                            // 生成栏目链接（兼容伪静态和动态模式）
                            $menu_link = $is_static
                                ? "/list/list-{$wbtreeid}.htm"
                                : "list.php?wbtreeid={$wbtreeid}";
                            ?>
                            <a href="<?= htmlspecialchars($menu_link) ?>">
                                <?= htmlspecialchars($menu_name) ?>
                            </a>

                        </li>
                    </ol>
                </nav>

                <!-- 文章内容 -->
                <article>
                    <div class="d-flex justify-content-between text-muted mb-4">
                        <span>作者：<?= htmlspecialchars($article['author'] ?? '') ?></span>
                        <span>发布日期：<?= date('Y-m-d H:i', $article['time'] ?? 0) ?></span>
                    </div>

                    <div class="article_content" id="article_content">
                        <?php
                        echo $article['content'];
                        ?>
                    </div>
                </article>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        function processTextNodes(node) {
            if (node.nodeType === Node.TEXT_NODE) {
                const text = node.nodeValue;
                const newText = text.replace(/([a-zA-Z0-9！@#¥%…….&*(){}|\\;:"'<>?/]+)/g, '<span style="font-size: 18px; font-family: Times New Roman;">$1</span>');

                const tempDiv = document.createElement('div');
                tempDiv.innerHTML = newText;

                while (tempDiv.firstChild) {
                    node.parentNode.insertBefore(tempDiv.firstChild, node);
                }
                node.parentNode.removeChild(node);
            } else {
                for (let i = 0; i < node.childNodes.length; i++) {
                    processTextNodes(node.childNodes[i]);
                }
            }
        }

        document.addEventListener('DOMContentLoaded', function() {
            const articleContent = document.getElementById('article_content');
            if (articleContent) {
                processTextNodes(articleContent);
            }
        });
    </script>
    <?php include('bottom.php'); ?>
</body>

</html>